/*
*	封装文件上传组件 demo
*/

var page_static = {};
var page_static_fn = {};
$(function(){

	try{
		page_static.uploader = [new xyd_upload(),new xyd_upload(),new xyd_upload()];//xyd_plupload-template.js
		page_static.btn_opens = ["openFile1","openFile2","openFile3"];
		page_static.containers = [$("#container1").get(0),$("#container2").get(0),$("#container3").get(0)];
		page_static.imageBox = [$("#user_uuid1"),$("#user_uuid2"),$("#user_uuid3")];
	}catch(e){
		throw new Error("object xyd_upload undefined");
		return;
	}
	
	page_static.uploader[0].settings = {
		url : 'http://localhost:8080/plupload/upload',
		type : 'image',
		size : '5mb',
		swf : xyd.sys.plupload.js.swf,
		xap : xyd.sys.plupload.js.xap,
		btn_openFileId : page_static.btn_opens[0],	
		node_box : page_static.containers[0],
		multi_selection : false
	};
	page_static.uploader[0].init(page_static.uploader[0].settings);
	
	page_static.uploader[1].settings = {
		url : 'http://localhost:8080/plupload/upload',
		type : 'image',
		size : '5mb',
		swf : xyd.sys.plupload.js.swf,
		xap : xyd.sys.plupload.js.xap,
		btn_openFileId : page_static.btn_opens[1],	
		node_box : page_static.containers[1],
		multi_selection : false
	};
	page_static.uploader[1].init(page_static.uploader[1].settings);
	
	page_static.uploader[2].settings = {
		url : 'http://localhost:8080/plupload/upload',
		type : 'image',
		size : '5mb',
		swf : xyd.sys.plupload.js.swf,
		xap : xyd.sys.plupload.js.xap,
		btn_openFileId : page_static.btn_opens[2],	
		node_box : page_static.containers[2],
		multi_selection : false
	};
	page_static.uploader[2].init(page_static.uploader[2].settings);
	
	
	/*************************************************************************
	*	事件控制区
	*************************************************************************/
	
	//第一区
	page_static.uploader[0].bindEvent("FilesAdded",function(up,files){
		$.each(up.files, function (i, f) {
			if (up.files.length <= 1) {return;}
			up.removeFile(f);
		});
		page_static_fn.FileChanged(page_static.containers[0],up.files);
		page_static_fn.createFilesUI(page_static.uploader[0],page_static.imageBox[0],up.files);
		setTimeout(function(){
			up.start();
		},300);
		
	});

	page_static.uploader[0].bindEvent("BeforeUpload",function(up,file){
		var plup = page_static.uploader[0].getPlupUploadObject();
		var f = plup.getFile(file.id);
		plup.setOption({multipart_params:{uuid:f.id+f.name}});
		page_static_fn.FileStateAnimate(file);
	});
	
	page_static.uploader[0].bindEvent("UploadFile",function(up,file){
		page_static_fn.FileStateAnimate(file);
	});
	page_static.uploader[0].bindEvent("FileUploaded",function(up,file,responseObject){
		if(file.status===5 && responseObject.status===200){
			if(responseObject.response==null){
				layer.msg("服务器返回null",{time:2000});
			}
			//这里可以给file文件设置url
		}
		page_static_fn.FileStateAnimate(file);
	});
	page_static.uploader[0].bindEvent("Error",function(up,err){
		page_static_fn.FileStateAnimate(err.file);
		var json = page_static.uploader[0].formatErr(err);
		layer.msg(json.err,{time:2000});
	});
	
	
	//第二区
	page_static.uploader[1].bindEvent("FilesAdded",function(up,files){
		$.each(up.files, function (i, f) {
			if (up.files.length <= 1) {return;}
			up.removeFile(f);
		});
		page_static_fn.FileChanged(page_static.containers[1],up.files);
		page_static_fn.createFilesUI(page_static.uploader[1],page_static.imageBox[1],up.files);
		setTimeout(function(){
			up.start();
		},300);
		
	});
	page_static.uploader[1].bindEvent("BeforeUpload",function(up,file){
		var plup = page_static.uploader[1].getPlupUploadObject();
		var f = plup.getFile(file.id);
		plup.setOption({multipart_params:{uuid:f.id+f.name}});
		page_static_fn.FileStateAnimate(file);
	});
	page_static.uploader[1].bindEvent("UploadFile",function(up,file){
		page_static_fn.FileStateAnimate(file);
	});
	page_static.uploader[1].bindEvent("FileUploaded",function(up,file,responseObject){
		if(file.status===5){
			//这里可以给file文件设置url
		}
		page_static_fn.FileStateAnimate(file);
	});
	page_static.uploader[1].bindEvent("Error",function(up,err){
		page_static_fn.FileStateAnimate(err.file);
		var json = page_static.uploader[1].formatErr(err);
		layer.msg(json.err,{time:2000});
	});
	
	
	//第三区
	page_static.uploader[2].bindEvent("FilesAdded",function(up,files){
		$.each(up.files, function (i, f) {
			if (up.files.length <= 1) {return;}
			up.removeFile(f);
		});
		page_static_fn.FileChanged(page_static.containers[2],up.files);
		page_static_fn.createFilesUI(page_static.uploader[2],page_static.imageBox[2],up.files);
		setTimeout(function(){
			up.start();
		},300);
	});
	page_static.uploader[2].bindEvent("BeforeUpload",function(up,file){
		var plup = page_static.uploader[2].getPlupUploadObject();
		var f = plup.getFile(file.id);
		plup.setOption({multipart_params:{uuid:f.id+f.name}});
		page_static_fn.FileStateAnimate(file);
	});
	page_static.uploader[2].bindEvent("UploadFile",function(up,file){
		page_static_fn.FileStateAnimate(file);
	});
	page_static.uploader[2].bindEvent("FileUploaded",function(up,file,responseObject){
		if(file.status===5){
			//这里可以给file文件设置url
		}
		page_static_fn.FileStateAnimate(file);
	});
	page_static.uploader[2].bindEvent("Error",function(up,err){
		page_static_fn.FileStateAnimate(err.file);
		var json = page_static.uploader[2].formatErr(err);
		layer.msg(json.err,{time:2000});
	});
	
	
	/*************************************************************************
	*	节点操作区
	*************************************************************************/
	
	//根据Files创建节点
	page_static_fn.createFilesUI = function(upload,elNode,files){
		elNode.html("");
		plupload.each(files, function(file) {
			var fileFix = String(upload.getFileTypeFx(file.name)).toLowerCase();
			var newIcon;
			if(fileFix!='jpg' && fileFix!=='gif' && fileFix!=='png'){
				newIcon = $("<i></i>");
				newIcon.addClass("ace-icon "+upload.options.uploadTypeIcon.none);
				$(newIcon).css({
					"width":"100px",
					"height":"100px",
					"color":"#999",
					"font-size":"4em",
					"padding-top":"23px"
				});
				newIcon = newIcon.prop("outerHTML");
			}else{
				newIcon = "<img id=\"images-file-"+file.id+"\"/>";
			}

			var newHTML = $("<div class=\"imagesList\" id=\"file-" + file.id + "\" title=\""+file.name+"\">"
							+ "<div class=\"btn-file-del\" title=\"删除文件\"><i class=\"ace-icon fa fa-trash-o fa-1x icon-only\" onclick=\"page_static_fn.FileRemove('"+file.id+"')\"></i></div>"
							+ newIcon
						+ "</div>");
			if(fileFix=='jpg' || fileFix=='gif' || fileFix=='png'){
				upload.previewImage(file, function (imgsrc) {
					$('#images-file-' + file.id).attr("src",imgsrc);
				})
			}

			elNode.append(newHTML);

			elNode.hover(function(){
				$(this).find(".btn-file-del").slideDown(200);
			},function(){
				$(this).find(".btn-file-del").hide();
				
			});
			
			page_static_fn.FileStateAnimate(file);

		});

	}
	
	//文件队列变化时
	page_static_fn.FileChanged = function(node,files){
		if(files.length===1){
			$(node).find(".openFileState").html(files[0].name);
		}else{
			$(node).find(".openFileState").html("");
		}
	}
	
	//单文件实时状态
	page_static_fn.FileStateAnimate = function(file){
		var el = $("#file-"+file.id);
		var box,dom;
		if(el.find(".ico-file-state-box").length<1){
			dom = $("<div></div>");
			box = $("<div></div>");
			
			el.append(box);
			el.append(dom);
		}else{
			box = el.find(".ico-file-state-box");
			dom = el.find(".ico-file-state-bean");
			box.attr("class","");
			dom.attr("class","");
		}

		box.addClass("ico-file-state-box");
		dom.addClass("ico-file-state-bean");
		
		var status = file.status;
		//status = 2;
		var icon,color;
		if(status>1 || status<6){
			icon = $("<i></i>"),color = {"color":"#ffffff"};
		}

		if(status===1){
			//准备就绪
		}else if(status===2){
			//正在上传
			icon.addClass("ace-icon fa fa-refresh fa-spin fa-1x fa-fw");
			icon.attr("title","正在上传...");
			color = {"color":"#1094FA"};
			box.addClass("uploadFile-state2");
			dom.addClass("ico-file-state-bean-s2");
		}else if(status===4){
			//上传失败
			box.addClass("uploadFile-state4");
			icon.addClass("ace-icon fa fa-refresh");
			icon.attr("onclick","page_static_fn.newStartFile('"+file.id+"')");
			icon.attr("title","重新上传");
		}else if(status===5){
			//上传成功
			icon.addClass("ace-icon glyphicon glyphicon-ok");
			icon.attr("title","上传成功");
			box.addClass("uploadFile-state5");
		}else{
			
		}

		dom.css(color).html(icon.prop("outerHTML"));
	}
	
	//重新上传
	page_static_fn.newStartFile = function(id){
		var plup = upload.getPlupUploadObject();
		var f = plup.getFile(id);
		plup.setOption({multipart_params:{uuid:f.id+f.name}});
		upload.newStartFile(id);
	}
	
	//删除文件
	page_static_fn.FileRemove = function(id){
		$("#file-"+id).fadeOut("fast", function (){
			$(this).remove();
		});

	}
	
	//取出上传成功的文件
	page_static_fn.getUploadSucceed = function(){
		var filesNode = $("#FileList").find(".imagesList");
		var str = "";
		$.each(filesNode,function(){
			var $this = $(this);
			var id = $this.attr("id");
			id = id.replace("file-", "");
			var file = upload.getFile(id);
			if(file!==undefined && file.status===5){
				str += file.newFileName+",";
			}
		});
		
		if(str!==""){
			str = str.substring(0,str.length-1);
		}
		return str;
	}


	//取出网络图片
	page_static_fn.getNetFiles = function(){
		var filesNode = image_view.find(".imagesList");
		var str = "";
		$.each(filesNode,function(){
			var $this = $(this);
			var id = $this.attr("id");
			id = id.replace("file-", "images-file-");
			str += $this.find("#"+id).attr("src")+",";
		});
		if(str!==""){
			str = str.substring(0,str.length-1);
		}
		return str;
	}
	
	
	//取出地址
	page_static_fn.publicGetFiles = function(){
		var a = $("#myTab4").find("li");
		var submitClass;
		$.each(a,function(){
			var $this = $(this);
			var ref = $this.attr("class");
			if(ref=='active'){
				submitClass = $this.find("a").attr("href");
			}
		});
		if(submitClass=="#home4"){
			return page_static_fn.getUploadSucceed();
		}else if(submitClass=="#profile4"){
			return page_static_fn.getNetFiles();
		}
		return undefined;
	}
});


